//==============================================================================
#include "Message.h"

//==============================================================================
Message::Message(void)
{
	setType(UNKNOWN_MESSAGE_TYPE);
	setData("");
}
//------------------------------------------------------------------------------
Message::Message(ByteBuffer* buffer)
{
	unmarshal(buffer);
}
//------------------------------------------------------------------------------
Message::Message(MESSAGE_TYPE type, string data)
{
	setType(type);
	setData(data);
}
//------------------------------------------------------------------------------
Message::~Message(void)
{
}
//------------------------------------------------------------------------------
void Message::setType(MESSAGE_TYPE type)
{
	this->type = type;
}
//------------------------------------------------------------------------------
MESSAGE_TYPE Message::getType(void)
{
	return this->type;
}
//------------------------------------------------------------------------------
MESSAGE_TYPE Message::toMessageType(int num)
{
	MESSAGE_TYPE MessageType = UNKNOWN_MESSAGE_TYPE;
	switch(num)
	{
		case NEW_PLAYER_SESSION_REQUEST:
			MessageType = NEW_PLAYER_SESSION_REQUEST;
			break;
		case NEW_OBSERVER_SESSION_REQUEST:
			MessageType = NEW_OBSERVER_SESSION_REQUEST;
			break;
		case NEW_SESSION_REQUEST_ACCEPTED:
			MessageType = NEW_SESSION_REQUEST_ACCEPTED;
			break;
		case NEW_SESSION_REQUEST_REJECTED:
			MessageType = NEW_SESSION_REQUEST_REJECTED;
			break;
		case START_GAME:
			MessageType = START_GAME;
			break;
		case GAME_OVER:
			MessageType = GAME_OVER;
			break;
		case SESSION_END:
			MessageType = SESSION_END;
			break;
		case SESSION_READY:
			MessageType = SESSION_READY;
			break;
		case GAME_FILES_REQUEST:
			MessageType = GAME_FILES_REQUEST;
			break;
		case SCENE_STATE_REQUEST:
			MessageType = SCENE_STATE_REQUEST;
			break;
		case SCENE_STATE:
			MessageType = SCENE_STATE;
			break;
		case EVENT_MOVE_UP_ON:
			MessageType = EVENT_MOVE_UP_ON;
			break;
		case EVENT_MOVE_UP_OFF:
			MessageType = EVENT_MOVE_UP_OFF;
			break;
		case EVENT_MOVE_DOWN_ON:
			MessageType = EVENT_MOVE_DOWN_ON;
			break;
		case EVENT_MOVE_DOWN_OFF:
			MessageType = EVENT_MOVE_DOWN_OFF;
			break;
		case EVENT_ROT_RIGHT_ON:
			MessageType = EVENT_ROT_RIGHT_ON;
			break;
		case EVENT_ROT_RIGHT_OFF:
			MessageType = EVENT_ROT_RIGHT_OFF;
			break;
		case EVENT_ROT_LEFT_ON:
			MessageType = EVENT_ROT_LEFT_ON;
			break;
		case EVENT_ROT_LEFT_OFF:
			MessageType = EVENT_ROT_LEFT_OFF;
			break;
		case EVENT_SHOOT:
			MessageType = EVENT_SHOOT;
			break;
	}
	return MessageType;
}
//------------------------------------------------------------------------------
void Message::setData(string data)
{
	this->data = data;
}
//------------------------------------------------------------------------------
string Message::getData(void)
{
	return this->data;
}
//------------------------------------------------------------------------------
void Message::marshal(ByteBuffer* buffer)
{
	buffer->clear();
	buffer->put("|");
	buffer->put(getType());
	buffer->put("|");
	buffer->put(getData());
}
//------------------------------------------------------------------------------
void Message::unmarshal(ByteBuffer* buffer)
{
	Tokenizer tokenizer(buffer->getAll(), "|");
	tokenizer.nextToken();
	setType(toMessageType(atoi(tokenizer.getToken().c_str())));
	tokenizer.nextToken();
	setData(tokenizer.getToken());
}
//------------------------------------------------------------------------------
string Message::toString(void)
{
	string str("");
	stringstream ss;
	ss << getType();
	str.append("[ ").append(ss.str()).append("|").append(getData());
	str.append(" ]");
	return str;
}